<?
namespace app\api\controller;
use think\Controller;
use think\Cookie;
use think\Request;
use think\Db;

   /*
    *@method mixed jacechen
    *@time 18.10.30
    */
class Game extends Base{
   /* public function _initialize(){
		header("Access-Control-Allow-Origin: *");
    	header("Access-Control-Allow-Methods: OPTIONS,HEAD,GET,POST,PUT,PATCH,DELETE");
   		header("Access-Control-Allow-Headers:Content-Type,Content-Range,Content-Disposition");
   		set_time_limit(0);
	}*/
    /*
    *@param mixed 客户微信基本信息
    *@time 18.10.30
    *获取前端发送过来的数据
    */
    public function login(){
       $param =  $this->request->post("data","",'htmlspecialchars_decode');
       $data=json_decode($param,true);
       $openid = $data['openid'];
       if(empty($openid)){
           return json(rel(-1,'openid不能为空'));
       }
           $info = Db::name('users')->where(array('openid'=>$openid))->find();
          if($info){
            $add['uid'] = $info['id'];
              return json(rel(1,'操作成功',$add['uid']));
          }else{
              $map['integral'] = 0;
              $map['name'] = 0;
              $map['openid'] =$openid;
              $map['wechatname'] = 0;
              $map['wechatphone']= 0;
              $map['level'] = 0;
              $map['stat'] = 0;
              $map['del'] = 0;
              $map['makecouponnum'] = 0;
              $map['ctime'] = time();
              $add = Db::name('users')->insertGetId($map);
          }
        if($add){
            return json(rel(1,'操作成功',$add));
        }else{
            return json(rel(-1,'操作失败'));
        }
    }
/*
    *获取H5微信基本信息->获取令牌*/
/*    public function acctoken(){
        //指定保存文件位置
	if(!is_dir('./access_token/')){
		mkdir(iconv("GBK","UTF-8",'./access_token/'),0777,true);
    }
    $opendId = $this->getOpenId();
	$file = './access_token/token';
	if(file_exists($file)){
		$content = file_get_contents($file);
		$cont = json_decode($content);
		if((time()-filemtime($file)) < $cont->expires_in){   //当前时间-文件创建时间<token过期时间
			return $cont->access_token;
		}
	}
	$curl = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$opendId.'&secret='.$this->appsecret;
	$content = $this->_request($curl);
	file_put_contents($file,$content);
	$cont = json_decode($content);
	return $cont->access_token;
    }*/
//玩游戏关卡 
 /*
 *  @判断当前关卡 根据传过来的数据 来进行判断
 *
 **/
   public function gamesLevel(){
       $param =  $this->request->post("data","",'htmlspecialchars_decode');
       $data=json_decode($param,true);
       $uid = $data['uid'];
       if(empty($uid)){
           return json(rel(-1,'uid不能为空'));
       }
       if(empty($data['levelNum'])){
           return json(rel(-1,'闯关数不能为空'));
       }
      //print_r($uid);die;
            $uInfo = Db::name('level')->where(array('uid'=>$uid))->field('id,levelnum,levelchild,status')->find();
       //print_r($param['levelnum']);
         // print_r($uInfo['levelnum']);die;
            if(!empty($uInfo)){
                //分享
                if(!empty($data['sharingFriend'])){
                    $map['levelnum'] = $data['levelNum'];
                    $mase['level'] = $data['levelNum'];
                    //$mase
                    //$map['levelchild'] = $param['levelChild'];
                    $map['status'] = 1;
                    $map['uid'] = $uid;
                    $rse = Db::name('users')->where(array('id'=>$uid))->update($mase);
                    $rel = Db::name('level')->where(array('id'=>$uInfo['id']))->update($map);
                }else{
                    if($data['levelNum']>$uInfo['levelnum']){
                        $map['levelnum'] = $data['levelNum'];
                        // $map['levelchild'] = $param['levelchild'];
                        $map['status'] = 1;
                        $map['uid'] = $uid;
                        $mas['level'] = $data['levelNum'];
                        $mas['utime'] = time();
                        if(!empty($data['money'])){
                            $mas['integral'] = $data['money'];
                        }
                        $rel = Db::name('level')->where(array('id'=>$uInfo['id']))->update($map);
                        $rrs = Db::name('users')->where(array('id'=>$uid))->update($mas);
                        if($rel&&$rrs){
                            return json(rel(1,'操作成功'));
                        }else{
                            return json(rel(-1,'操作失败'));
                        }
                    }else{
                        return json(rel(1,'操作成功'));
                    }
                }

        }else{
                $map['levelnum'] = $data['levelNum'];
                $map['status'] = 1;
                $map['ctime'] = time();
                $map['uid'] = $uid;
                $mas['level'] = $data['levelNum'];
                $mas['utime'] = time();
                if(!empty($data['money'])){
                    $mas['integral'] = $data['money'];
                }
                $rel = Db::name('level')->where(array('id'=>$uInfo['id']))->insertGetId($map);
                $rrs = Db::name('users')->where(array('id'=>$uid))->update($mas);
                if($rel&&$rrs){
                    return json(rel(1,'操作成功',$rel));
                }else{
                    return json(rel(-1,'操作失败'));
                }
            }
   }
   //当前关卡
public function nowlevel(){
    $param =  $this->request->post("data");
    $datas = json_decode($param,true);
   // print_r($datas);die;
    $level = Db::name('users')->where(array('id'=>$datas['uid']))->find();
    if($level['level']==0){
        $level['level'] = 1;
    }
    if($level){
        return json(rel(1,'获取成功',$level['level']));
    }else{
        return json(rel(-1,'暂无记录'));
    }
}
   //核销
    public function writeOff(){
        $param =  $this->request->post("data");
        $datas = json_decode($param,true);
        //$uid = $datas['uid'];
        $carid = $datas['cardId'];
        //$rel = Db::name('users_card')->where(array('id'=>$carid))->find();
        //$number = Db::name('users')->where(array('id'=>$uid))->find();
        if(!empty($datas['doctorsName'])){
            $doctors = $datas['doctorsName'];
        }else{
            return json(rel(-1,'请填医生名字'));
        }
            $res =  Db::name('users_card')->where(array('id'=>$carid))->update(['status'=>1,'doctors'=>$doctors]);
        if($res){
            return json(rel(1,'操作成功'));
            //return json_encode(rel(-1,'操作失败'));
        }else{
            return json(rel(-1,'操作失败'));
        }
    }
    //分享
    public function Wefx(){
        $param =  $this->request->post("data","",'htmlspecialchars_decode');
        $data=json_decode($param,true);
      $aa = $this->WeChatFX();
      $data['appid'] = $aa['appId'];
      $data['timestamp'] = $aa['timestamp'];
      $data['noncestr'] = $aa['noncestr'];
      $data['jsapi_ticket'] = $aa['jsapi_ticket'];
      $data['sign'] = $aa['sign'];
        return json(rel(1,'操作成功',$data));
    }
    //我的积分
    public function myIntegral(){
        $param =  $this->request->post("data","",'htmlspecialchars_decode');
        $data=json_decode($param,true);
        $uid = $data['uid'];
        $rel = db::name('users')->where(array('id'=>$uid))->find();
        if($rel){
            return json(rel(1,'获取成功',$rel['integral']));
        }else{
            return json(rel(-1,'获取失败'));
        }
    }

    //我的卡卷
   public function cardlist()
   {
       $param =  $this->request->post("data","",'htmlspecialchars_decode');
       $data=json_decode($param,true);
       $uid = $data['uid'];
       $rel = db::name('users_card')->where(array('userid'=>$uid))->select();
       foreach ($rel as $k=>$v){
         $datae[$k]['id'] = $v['Id'];
         $datae[$k]['status'] = $v['status'];
       }
      // $card  = db::name('users')->where(array('id'=>$rel['carid']))->find();
       if($rel){
           return json(rel(1,'获取成功',$datae));
       }else{
           return json(rel(-1,'获取失败'));
       }
   }
   //领劵
    public function usecard(){
        $param =  $this->request->post("data","",'htmlspecialchars_decode');
        $data=json_decode($param,true);
        $maps['userid'] = $data['uid'];
        $maps['time'] = time();
        $maps['status'] = 2;
        //看积分
        $user = db::name('users')->where(array('id'=>$data['uid']))->find();
        if($user['integral']<100){
            return json(rel(-1,'您当前积分不足'));
        }else{
            $rel = Db::name('users')->where(array('id'=>$data['uid']))->setdec('makecouponnum',100);
            $card=db::name('users_card')->where(array('id'=>$data['uid']))->insertGetId($maps);
            //$rel = db::name('users')->where(array('id'=>$data['uid']))->setInc('makecouponnum');
            if($rel&&$card){
                return json(rel(1,'添加成功'));
            }else{
                return json(rel(-1,'添加失败'));
            }
        }

    }
}